package com.zhl.emp_manage.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhl.emp_manage.entity.Emp;
import com.zhl.emp_manage.service.EmpService;
import com.zhl.emp_manage.utils.Results;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "员工信息管理")
@RestController
@RequestMapping("/emp-manage/emp")
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 查询所有
     *
     * @return
     */
    @ApiOperation("查询所有员工信息")
    @GetMapping("/emps")
    public Results getEmpAll() {
        List<Emp> empAll = empService.getEmpAll();
        return Results.ok().data("list", empAll);
    }

    /**
     * 添加
     *
     * @param emp
     * @return
     */
    @ApiOperation("添加员工信息")
    @PostMapping("/emps")
    public Results addEmp(@RequestBody Emp emp) {
        boolean b = empService.addEmp(emp);
        return Results.ok().message("添加成功");
    }

    /**
     * 修改
     *
     * @param emp
     * @return
     */
    @ApiOperation("修改员工信息")
    @PostMapping("/emps/{id}")
    public Results updateEmp(@RequestBody Emp emp, @PathVariable Integer id) {
        boolean b = empService.updateEmp(emp);
        return Results.ok().message("修改成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除员工信息")
    @DeleteMapping("/emps/{id}")
    public Results deleteEmp(@PathVariable Integer id) {
        boolean b = empService.deleteEmp(id);
        return Results.ok().message("删除成功");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询员工信息")
    @GetMapping("/emps/{id}")
    public Results getEmpById(@PathVariable Integer id) {
        Emp e = empService.getById(id);
        return Results.ok().data("emp", e);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/emps/pages")
    public Results getEmp(@RequestParam(value = "page") Integer page, @RequestParam(value = "limit") Integer limit) {
        PageHelper.startPage(page, limit);
        List<Emp> empList = empService.getEmp(page, limit);
        PageInfo<Emp> pageInfo = new PageInfo(empList);
        return Results.ok().data("pageInfo", pageInfo);
    }

    /**
     * 模糊查询
     *
     * @param name
     * @return
     */
    @ApiOperation("模糊查询")
    @GetMapping("/emps/empName")
    public Results getEmpLike(@RequestParam(value = "name") String name) {
        List<Emp> empLike = empService.getEmpLike(name);
        return Results.ok().data("empLike", empLike);
    }

    /**
     * 查询部门下的所有员工
     *
     * @param deptNo
     * @return
     */
    @ApiOperation("查询部门下的所有员工")
    @GetMapping("/emps/empByDept")
    public Results getEmpByDeptNo(@RequestParam(value = "deptNum") Integer deptNo) {
        List<Emp> empByDeptNo = empService.getEmpByDeptNo(deptNo);
        return Results.ok().data("empByDeptNo", empByDeptNo);
    }

    /**
     * 动态返回查询结果
     *
     * @param wrapper
     * @param id
     * @return
     */
    @ApiOperation("动态返回查询结果")
    @GetMapping("/emps/empWrapper/{id}")
    public Results queryWrapper(@RequestParam(value = "wrapper") List<String> wrapper, @PathVariable Integer id) {
        Emp empWrapper = empService.queryWrapper(wrapper, id);
        return Results.ok().data("empWrapper", empWrapper);
    }
}
